package com.tencent.welink.api.cos;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.welink.api.OpenApiTokenChange;
import com.tencent.welink.api.base.WelinkOpenApi;
import com.tencent.welink.config.BussinessConfig;
import com.tencent.welink.util.CrontabUtil;
import com.tencent.welink.util.HttpPoolUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
 * desc: 客户端调用该接口获取下载地址，通过HTTP GET请求下载地址的方式将文件流转成文件。
 * Created by junchihe on 2020/6/4.
 */
public class DownloadTemporaryFile extends WelinkOpenApi {
    private static final Logger log = LogManager.getLogger(DownloadTemporaryFile.class);
    public DownloadTemporaryFile() {
        super("/common/fileservice/downloadTemporaryFile",true);
    }
    @Override
    public void displayResult(String result) {

    }

    public static void main(String[] args) {
        new BussinessConfig();
        CrontabUtil.addTaskInRate(new OpenApiTokenChange(), BussinessConfig.loginDurationMinutes,true);
        DownloadTemporaryFile downloadTemporaryFile =   new DownloadTemporaryFile();
        Map<String,Object> map = downloadTemporaryFile.getExtendsPara();

        map.put("file_id","55e45048-e854-4cfc-bc3e-d7beb1e91497");
        String reslut = downloadTemporaryFile.DoHttp();
        CrontabUtil.shutdown();

        JSONObject jsonObject = JSON.parseObject(reslut);
        jsonObject = jsonObject.getJSONObject("data");
        String fileId = jsonObject.getString("filename");
        String upload_url = jsonObject.getString("downloadUrl");
        log.info("fileid:{}",fileId);
        log.info("url:{}",upload_url);
        log.info(HttpPoolUtils.httpGet(upload_url,null));
    }
}
